package com.litidea.actions;

import static com.litidea.commons.keys.SystemKeys.ATTACHMENTS_UPLOAD_URL;
import static com.litidea.commons.keys.SystemKeys.REPORT_UPLOAD_URL;
import static com.litidea.commons.paramkeys.ChangeParamKeys.SEARCH_CHANGE_HISTORY;

import java.util.Map;

import com.litidea.commons.utils.FileUtils;
import com.litidea.dao.ChangeHistoryDAO;
import com.opensymphony.xwork2.Action;

public class ChangeHistoryManageAction extends BaseAction implements Action {

	/**
	 * 序列码
	 */
	private static final long serialVersionUID = 968674751758541095L;

	private ChangeHistoryDAO changeHistoryDAO;

	public String getChangeHistory() {
		Map<String, String> params = getParameters(SEARCH_CHANGE_HISTORY);
		try {
			fillPageParams(params);
			return responseAjax(changeHistoryDAO.getChangeHistoryPageList(
					params).toString());
		} catch (Exception e) {
			errorInfo = e.getMessage();
			e.printStackTrace();
		}
		return null;
	}
	
	public String getExternalChangeHistory() {
		Map<String, String> params = getParameters(SEARCH_CHANGE_HISTORY);
		try {
			fillPageParams(params);
			return responseAjax(changeHistoryDAO.getExternalChangeHistoryPageList(
					params).toString());
		} catch (Exception e) {
			errorInfo = e.getMessage();
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 修改附件
	 * 
	 * @return
	 */
	public String changeAttachments() {
		Map<String, String> params = getParameters(SEARCH_CHANGE_HISTORY);
		String success = null, errorInfo = null;
		try {
			String flag = params.get("type");
			String type;
			if (flag.equals("1") || flag.equals("2") || flag.equals("3"))
				type = REPORT_UPLOAD_URL;
			else
				type = ATTACHMENTS_UPLOAD_URL;

			// 上传验证报告
			String saveFileFullName = this.saveUploadFile(type);
			params.put("attachmentURL", saveFileFullName);

			success = changeHistoryDAO.changeAttachment(params);

			// 删除老文件
			String oldFile = params.get("removedFile");
			if (oldFile != null) {
				FileUtils.deleteFileOrDirectory(oldFile);
			}
		} catch (Exception e) {
			errorInfo = e.getMessage();
		}
		return normalResponse(errorInfo, success);
	}

	public ChangeHistoryDAO getChangeHistoryDAO() {
		return changeHistoryDAO;
	}

	public void setChangeHistoryDAO(ChangeHistoryDAO changeHistoryDAO) {
		this.changeHistoryDAO = changeHistoryDAO;
	}

}
